package net.keebook.webapp.util;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import net.keebook.webapp.dao.AccountConfirmationDao;
import net.keebook.webapp.dao.SettingDao;
import net.keebook.webapp.dao.UserDao;
import net.keebook.webapp.entity.AccountConfirmation;
import net.keebook.webapp.entity.User;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class CheckSubscriptions implements Job {
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			Properties properties = new Properties();
			properties.load(getClass().getResourceAsStream("/settings.properties"));
			final String moduleName = properties.getProperty("moduleName");
			
			Context initCtx = new InitialContext();
			final AccountConfirmationDao accountConfirmationDao = (AccountConfirmationDao)initCtx.lookup("java:global/"+moduleName+"/AccountConfirmationDao");
			final UserDao userDao = (UserDao)initCtx.lookup("java:global/"+moduleName+"/UserDao");
			final SettingDao settingDao = (SettingDao)initCtx.lookup("java:global/"+moduleName+"/SettingDao");
			
			Logger.getLogger(this.getClass()).info("Checking for unconfirmed accounts");
			
			Timestamp ts = new Timestamp(new Date().getTime() - (1000 * 60 * 60 * 24));
			List<AccountConfirmation> list = accountConfirmationDao.getOlderThan(ts);
			
			int i = 0;
			for (AccountConfirmation ac : list) {
				User user = ac.getUser();
				if (user != null && !user.getConfirmed()) {
					user.setEnabled(false);
					userDao.update(user);
					
					ac.setPermanent(true);
					accountConfirmationDao.update(ac);
					
					String url = settingDao.get(SettingId.PUBLIC_URL).getValue() + "/services/confirm/" + ac.getExposableId();
					String message = MessageBundle.get().getString("mail.confirmationReminderMail");
					message = MessageFormat.format(message, url);
					
					Mail mail = new Mail(
							settingDao.get(SettingId.SMTP_HOST).getValue(), 
							settingDao.get(SettingId.SMTP_PORT).getLong().intValue(),
							settingDao.get(SettingId.SMTP_AUTH).getBoolean(),
							settingDao.get(SettingId.SMTP_USERNAME).getValue(),
							settingDao.get(SettingId.SMTP_PASSWORD).getValue(),
							settingDao.get(SettingId.SMTP_ENABLE).getBoolean());
					try {
						mail.sendMail(
							settingDao.get(SettingId.SMTP_SENDER_ADDRESS).getValue(),
							"KeeBook",
							user.getEmail(),
							null,
							MessageBundle.get().getString("mail.confirmationReminderMailSubject"),
							message);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				i++;
			}
			
			Logger.getLogger(this.getClass()).info("Disabled " + i + " unconfirmed accounts");
		} catch (NamingException e) {
			Logger.getLogger(this.getClass()).error("Error looking up context", e);
		} catch (IOException e) {
			Logger.getLogger(this.getClass()).error("Error loading settings.properties", e);
		}
	}
}
